/*
 * RestingPoseDialog.hpp
 *
 *  Created on: 04-12-2008
 *      Author: jimali
 */

#ifndef RWSIMPLUGIN_HPP_
#define RWSIMPLUGIN_HPP_

#include "Defines.hpp"

#ifdef __WIN32
#include <windows.h>
#endif

#include "ui_RWSimPlugin.h"
#include "OwnGraspTaskSimulator.hpp"

#include <rwsim/simulator/PhysicsEngineFactory.hpp>

#include <rwsim/dynamics/DynamicWorkCell.hpp>
#include <rwsim/dynamics/RigidBody.hpp>
#include <rwsim/simulator/ThreadSimulator.hpp>
#include <rwsim/util/MovingAverage.hpp>
#include <rw/graspplanning/GraspTable.hpp>

#include <rw/common/Ptr.hpp>
#include <rw/kinematics/State.hpp>
#include <rw/kinematics/FrameMap.hpp>
#include <rw/proximity/CollisionDetector.hpp>

#include <rwlibs/opengl/Drawable.hpp>

#include <rws/RobWorkStudioPlugin.hpp>

#include <rwsimlibs/ode/ODESimulator.hpp>

#include <rwlibs/task/Task.hpp>
#include <rwsim/simulator/GraspTaskSimulator.hpp>

#include <QObject>
#include <QtGui>
#include <QTimer>

struct UserContext {
	std::string _previousOpenDirectory;
};

/**
 * @brief A plugin for loading dynamic workcells and for doing simple
 * dynamics simulation using different physics engines.
 *
 *
 */
class RWSimPlugin : public rws::RobWorkStudioPlugin, private Ui::RWSimPlugin
    {
		Q_OBJECT
		Q_INTERFACES( rws::RobWorkStudioPlugin )
    public:
    	/**
    	 * @brief constructor
    	 */
        RWSimPlugin();

        /**
         * @brief destructor
         */
        virtual ~RWSimPlugin(){};

        /**
         * @copydoc RobWorkStudioPlugin::open
         */
        void open(rw::models::WorkCell* workcell);

        /**
         * @copydoc RobWorkStudioPlugin::close
         */
        void close();

        /**
         * @copydoc RobWorkStudioPlugin::initialize
         */
        void initialize();

        void stateChangedListener(const rw::kinematics::State& state);

        void stepCallBack(const rw::kinematics::State& state);

        void setupMenu(QMenu* menu);

        void openDWC();
        void statemachine();

    signals:
        void updateView();
        void updateDialog();

    private slots:
        void btnPressed();
        void changedEvent();

    protected:
    	void updateStatus();

    private:
    	rwlibs::task::CartesianTask::Ptr saveResultXML(rwlibs::task::CartesianTask::Ptr &tasks, const std::string & name, bool removeFailures = false);
    	void saveResults(rwlibs::task::CartesianTask::Ptr &tasks, int state);

        rw::common::Ptr<rwsim::dynamics::DynamicWorkCell> _dwc;
        rw::common::Ptr<rwsim::simulator::ThreadSimulator> _sim;

        rw::kinematics::State _state;
        rw::kinematics::State _startState;
        rwlibs::task::CartesianTask::Ptr _tasks;
        rwsim::simulator::OwnGraspTaskSimulator::Ptr _graspSim;

        int _AccState;
        int _curTask;
        std::vector<int> _successIDs;

        QAction *_openAction;
        QTimer *_timer;

        long _seed;
};


#endif /* RESTINGPOSEDIALOG_HPP_ */
